package com.ericsson.mna.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ericsson.mna.model.bs.UserRegisterServices;
import com.ericsson.mna.model.pojo.CreateUser;
import com.ericsson.mna.model.pojo.DBConf;

/**
 * Servlet implementation class UserController
 */
@WebServlet("/UserController")
public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pageName=request.getParameter("pageName");
		if(pageName.equalsIgnoreCase("registerUser"))
		{
			String name = request.getParameter("name");
			String contact = request.getParameter("contact");
			String domain = request.getParameter("domain");
			String role = request.getParameter("role");
			String sub_domain = request.getParameter("sub_domain");
			String primarySkill=request.getParameter("primarySkill");
			String secondarySkill=request.getParameter("secondarySkill");

			CreateUser cu=new CreateUser();
			cu.setName(name);
			cu.setContact(contact);
			cu.setDomain(domain);
			cu.setSub_domain(sub_domain);
			cu.setRole(role);
			cu.setPrimarySkill(primarySkill);
			cu.setSecondarySkill(secondarySkill);
			HttpSession session=request.getSession();
			session.setAttribute("CreateUserObj", cu);
			Properties prop=new Properties();
			prop.load(getServletContext().getResourceAsStream("/view/resources/prop/DbConf.properties"));
			DBConf dbconf=new DBConf();
			dbconf.setDriverName(prop.getProperty("driverName"));
			dbconf.setPassword(prop.getProperty("password"));
			dbconf.setUrl(prop.getProperty("url"));
			dbconf.setUserName(prop.getProperty("userName"));
			UserRegisterServices urs=new UserRegisterServices(dbconf);
			int abt=urs.insertUser(cu, (String)session.getAttribute("userId"));
			PrintWriter out=response.getWriter();
			if(abt>0)
			{
				//
				out.println("<script language=javascript>");
				out.println("<!--");
				out.println("alert('Details Inserted Sucessfully. User Id is :: "+ cu.getUserId()+"');");
				out.print("window.location='tab.jsp'");
				out.println("//-->");
				out.println("</script>");
			}
			else
			{
				out.println("<script language=javascript>");
				out.println("<!--");
				out.println("alert('Details Not Inserted Sucessfully. Please reinsert.. '");
				out.print("window.location='tab.jsp'");
				out.println("//-->");
				out.println("</script>");

			}





		}
	}

}
